﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Yunwei.DAL;
using Yunwei.Domain.Models;
using Yunwei.IBizLogic;

namespace Yunwei.BizLogic
{
    public class FoodIngredientService : BaseService<FoodIngredient>, IFoodIngredientService
    {
        public FoodIngredientService()
            : base(RepositoryFactory.FoodIngredientRepository)
        {
        }

        public FoodIngredient FindByIngredient(int food_Id, int ingredient_Id)
        {
            if (ingredient_Id == 0 || food_Id == 0) return null;

            IQueryable<FoodIngredient> ri = CurrentRepository.Entities;
            if (ingredient_Id > 0) ri = ri.Where(cm => cm.Ingredient_Id == ingredient_Id);
            if (food_Id > 0) ri = ri.Where(s => s.Food_Id == food_Id);

            return ri.FirstOrDefault();
        }

        public IQueryable<FoodIngredient> FindList(int number, int food_Id, int orderCode)
        {
            return FindPageList(out number, 1, number,  food_Id, orderCode);
        }

        public IQueryable<FoodIngredient> FindPageList(out int totalRecord, int pageIndex, int pageSize, int food_Id, int orderCode)
        {
            IQueryable<FoodIngredient> _commonModels = CurrentRepository.Entities;

            _commonModels = Order(_commonModels, orderCode);
            totalRecord = _commonModels.Count();
            return PageList(_commonModels, pageIndex, pageSize).AsQueryable();
        }

        public IQueryable<FoodIngredient> Order(IQueryable<FoodIngredient> entitys, int orderCode)
        {
            switch (orderCode)
            {
                //默认排序
                default:
                    entitys = entitys.OrderByDescending(cm => cm.Id);
                    break;
            }
            return entitys;
        }

    }
}
